package top.wintp.offlinedataanalysis.anlyser.mr;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import top.wintp.offlinedataanalysis.anlyser.dao.DimensionBrowserDAO;
import top.wintp.offlinedataanalysis.anlyser.dao.DimensionDateDAO;
import top.wintp.offlinedataanalysis.anlyser.dao.DimensionPlatformDAO;
import top.wintp.offlinedataanalysis.anlyser.dim.base.BrowserDimension;
import top.wintp.offlinedataanalysis.anlyser.dim.base.DateDimension;
import top.wintp.offlinedataanalysis.anlyser.dim.base.PlatformDimension;
import top.wintp.offlinedataanalysis.anlyser.domain.DimensionBrowserExample;
import top.wintp.offlinedataanalysis.anlyser.domain.DimensionDateExample;
import top.wintp.offlinedataanalysis.anlyser.domain.DimensionPlatformExample;
import top.wintp.offlinedataanalysis.util.MyBatisUtils;

/**
 * @author: pyfysf
 * <p>
 * @qq: 337081267
 * <p>
 * @CSDN: http://blog.csdn.net/pyfysf
 * <p>
 * @blog: http://wintp.top
 * <p>
 * @email: pyfysf@163.com
 * <p>
 * @time: 2019/9/6
 */
public class ConvertDimension {
    private static SqlSessionFactory sqlSessionFactory;


    static {
        sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
    }

    /**
     * 转换维度 判断指定维度是否存在 如不存在将执行插入 返回维度id  如果存在将直接返回id
     *
     * @param date
     * @return
     */
    public static int convertDateDimension(DateDimension date) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        DimensionDateDAO mapper = sqlSession.getMapper(DimensionDateDAO.class);
        DimensionDateExample example = new DimensionDateExample();
        example.createCriteria()
                .andYearEqualTo(date.getYear())
                .andSeasonEqualTo(date.getSeason())
                .andMonthEqualTo(date.getMonth())
                .andWeekEqualTo(date.getWeek())
                .andDayEqualTo(date.getDay())
                .andTypeEqualTo(date.getType());

        List<DateDimension> dateDimensions = mapper.selectByExample(example);

        if (dateDimensions == null || CollectionUtil.isEmpty(dateDimensions)) {
            //    没有数据执行插入
            mapper.insert(date);
            return date.getId();
        }

        //关闭资源
        sqlSession.close();
        return dateDimensions.get(0).getId();
    }

    /**
     * 转换平台信息
     *
     * @param platform
     * @return
     */
    public static int convertPlatformDimension(PlatformDimension platform) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        DimensionPlatformDAO mapper = sqlSession.getMapper(DimensionPlatformDAO.class);
        DimensionPlatformExample example = new DimensionPlatformExample();
        example.createCriteria()
                .andPlatformNameEqualTo(platform.getPlatformName());

        List<PlatformDimension> platformDimensions = mapper.selectByExample(example);

        if (platformDimensions == null || CollectionUtil.isEmpty(platformDimensions)) {
            //    没有数据执行插入
            mapper.insert(platform);
            return platform.getId();
        }

        //关闭资源
        sqlSession.close();
        return platformDimensions.get(0).getId();
    }

    /**
     * 转换浏览器维度
     *
     * @param browser
     * @return
     */
    public static int convertBrowserDimension(BrowserDimension browser) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        DimensionBrowserDAO mapper = sqlSession.getMapper(DimensionBrowserDAO.class);
        DimensionBrowserExample example = new DimensionBrowserExample();
        example.createCriteria()
                .andBrowserVersionEqualTo(browser.getBrowserVersion())
                .andBrowserNameEqualTo(browser.getBrowserName());

        List<BrowserDimension> browserDimensions = mapper.selectByExample(example);

        if (browserDimensions == null || CollectionUtil.isEmpty(browserDimensions)) {
            //    没有数据执行插入
            mapper.insert(browser);
            return browser.getId();
        }

        //关闭资源
        sqlSession.close();
        return browserDimensions.get(0).getId();
    }
}
